<template>
	<view class="add_outside">
		<xbd-pages-content title="室外下单">
			<template #content>
				<view class="content" :style="'minHeight:'+windowHeight+'px'">
					<view class="type">
						<text>电话</text>
						<uni-easyinput class="input" :clearable="false" v-model="formData.number"
							placeholder="请输入电话号码" />
					</view>
					<view class="type">
						<text>姓名</text>
						<uni-easyinput class="input" :clearable="false" v-model="formData.name" placeholder="请输入姓名" />
					</view>
					<view class="type">
						<text>收货站点</text>
						<xbd-select :carId.sync="type" :visible.sync="visible" :infoList="typeArr" @selectSite ="selectSite">
							<template>
								<view class="select" @click="visible = true">
									<text> {{type || '请选择站点'}} </text>
									<image class="image" src="@/static/images/health/right.svg" mode=""></image>
								</view>
							</template>
						</xbd-select>
					</view>
					<view class="type">
						<text>店铺</text>
						<text class="input text">{{formData.store}}</text>
					</view>
					<view class="bottom">
						<view class="button" @click="add()">添加订单</view>
					</view>
				</view>
			</template>
		</xbd-pages-content>
	</view>
</template>

<script>
	import { DeliveryOrder } from '@/api/index.js';
	import { mapState } from 'vuex';
	export default {
		data() {
			return {
				flag:true,
				visible: false,
				type:"",
				typeArr: [
				],
				windowHeight: 0,
				formData: {
					number: '',
					name: '',
					site: '',
					store: '榆林小保当-默认专车店铺'
				},
			};
		},
		methods: {
			selectSite(value){
				this.formData.site = value
			},
			add() {
				const reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
				const reg2 = /^[\u4e00-\u9fa5]{2,10}$/;
				if (!this.formData.number.length) {
					this.$showToast('请输入电话号');
				} else if (!reg.test(this.formData.number)) {
					this.$showToast('请输入正确格式电话号');
				} else if (!this.formData.name.length) {
					this.$showToast('请输入姓名');
				} else if (!reg2.test(this.formData.name)) {
					this.$showToast('请输入2-10位汉字');
				} else if (!this.formData.site.length) {
					this.$showToast('请选择收货站点');
				}else if(this.flag){
					this.flag = false
					this.submit()
				}
			},
			async getSite(){
				try {
					const res = await DeliveryOrder.getSiteList();
					this.typeArr = res.data.list
				} catch (err) {
					uni.showToast({
						title: err[0].errorMessage,
						icon: 'none'
					})
				}
			},
			async submit(val) {
				
				const info ={
					username:this.formData.name , //用户名
					phone: this.formData.number, //手机号
					site_id:this.formData.site //站点ID
				}
				const o = {
					obj: info
				}
				try {
					this.$showLoading('加载中');
					 const res = await DeliveryOrder.postCreateOrder(info);
					 uni.hideLoading();
					 if(res.success){
						 uni.showToast({
						 	title: '您的订单添加成功！',
						 	icon: 'none',
						 })
						 setTimeout(() => {
						 	uni.redirectTo({
						 		url: './outSideList'
						 	});
						 }, 1000)
					 }
					
				} catch (err) {
				}
			},
		},
		computed: {},
		onLoad() {
			this.getSite()
			uni.getSystemInfo({
				success: (res) => {
					this.windowHeight = res.windowHeight
				}
			});
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .uni-easyinput__content-input {
		text-align: right;
	}

	::v-deep .is-input-border {
		border: none;
	}

	::v-deep .is-disabled {
		background-color: #FFFFFF !important;
	}

	.content {
		padding: 40rpx;
		background-color: #FFFFFF;
		box-sizing: border-box;

		.type {
			box-sizing: border-box;
			padding: 20rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #E8E2E2;
			font-size: 28rpx;

			.text {
				margin: 20rpx;
			}

			.input {
				text-align: right;
				font-size: 28rpx !important;
				color: #394954 !important;
			}

			.select {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 28px;
				line-height: 28px;
				box-sizing: border-box;
				padding: 0 20rpx 0 20rpx;
				color: #999999 !important;

				.image {
					transform: rotate(90deg);
					width: 38rpx;
					height: 38rpx;
				}
			}
		}

		.bottom {
			display: flex;
			justify-content: center;
			margin-top: 80rpx;

			.button {
				// width: 240rpx;
				height: 70rpx;
				line-height: 70rpx;
				text-align: center;
				border-radius: 8rpx;
				font-size: 28rpx;
				color: #FFFFFF;
				background-color: #457AFF;
			}
		}
	}
</style>
